package o;

import android.annotation.SuppressLint;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnBufferingUpdateListener;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnErrorListener;
import android.media.MediaPlayer.OnInfoListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.FrameLayout;
import android.widget.SeekBar;
import android.widget.TextView;
import com.huawei.appgallery.common.media.widget.VideoPlayButton;
import com.huawei.appgallery.common.media.widget.VideoPlayButton.e;
import java.util.Formatter;
import java.util.Locale;
import o.ble.d;
import o.ble.j;

@SuppressLint({"HandlerLeak", "InflateParams"})
public class blq implements OnBufferingUpdateListener, OnCompletionListener, OnPreparedListener, OnErrorListener, OnInfoListener, Callback, OnClickListener {
    private TextView ʻ;
    private long ʼ = 0;
    private VideoPlayButton ʽ;
    private String ˊ;
    private b ˊॱ;
    private MediaPlayer ˋ;
    private boolean ˋॱ = false;
    private SurfaceHolder ˎ;
    private SurfaceView ˏ;
    private Formatter ˏॱ;
    private boolean ͺ = false;
    private View ॱ;
    private StringBuilder ॱˊ;
    private SeekBar ॱॱ;
    private Handler ॱᐝ = new Handler(this) {
        final /* synthetic */ blq ॱ;

        {
            this.ॱ = r1;
        }

        public void handleMessage(Message message) {
            if (message.what == 1000) {
                this.ॱ.ʽ();
                if (this.ॱ.ͺ) {
                    this.ॱ.ॱᐝ.sendEmptyMessageDelayed(1000, 1000);
                }
            } else if (message.what == 1001) {
                this.ॱ.ʻ();
            }
        }
    };
    private TextView ᐝ;

    public interface b {
        void I_();

        void ͺ();

        boolean ॱˊ();

        void ᐝ();

        void ᐝॱ();
    }

    public blq(FrameLayout frameLayout, SurfaceView surfaceView) {
        this.ॱ = LayoutInflater.from(frameLayout.getContext()).inflate(j.ˏ, null);
        this.ॱॱ = (SeekBar) this.ॱ.findViewById(o.ble.b.ˊ);
        this.ʽ = (VideoPlayButton) this.ॱ.findViewById(o.ble.b.ॱ);
        this.ʻ = (TextView) this.ॱ.findViewById(o.ble.b.ᐝ);
        this.ᐝ = (TextView) this.ॱ.findViewById(o.ble.b.ˏ);
        LayoutParams layoutParams = new FrameLayout.LayoutParams(-1, -2);
        layoutParams.gravity = 80;
        this.ॱ.setLayoutParams(layoutParams);
        frameLayout.addView(this.ॱ);
        this.ॱ.setVisibility(8);
        this.ʽ.setOnClickListener(this);
        surfaceView.setOnClickListener(this);
        surfaceView.setClickable(false);
        this.ˏ = surfaceView;
        this.ˎ = surfaceView.getHolder();
        this.ˎ.addCallback(this);
        this.ˎ.setType(3);
        this.ॱˊ = new StringBuilder();
        this.ˏॱ = new Formatter(this.ॱˊ, Locale.getDefault());
    }

    public void ˋ(b bVar) {
        this.ˊॱ = bVar;
    }

    public void surfaceCreated(SurfaceHolder surfaceHolder) {
        try {
            this.ˋ = new MediaPlayer();
            this.ˋ.setDisplay(surfaceHolder);
            this.ˋ.setAudioStreamType(3);
            this.ˋ.setScreenOnWhilePlaying(true);
            this.ˋ.setOnBufferingUpdateListener(this);
            this.ˋ.setOnPreparedListener(this);
            this.ˋ.setOnCompletionListener(this);
            this.ˋ.setOnInfoListener(this);
            this.ˋ.setOnErrorListener(this);
            this.ˋॱ = false;
            if (this.ˊ != null) {
                ˏ(this.ˊ);
            }
        } catch (Throwable e) {
            blf.ˋ.ˊ("VideoController", "surfaceCreated error", e);
        }
    }

    @SuppressLint({"InlinedApi"})
    public void ˏ(String str) {
        if (this.ˋ == null) {
            this.ˊ = str;
            return;
        }
        try {
            this.ˋ.setDataSource(str);
            this.ˋ.prepareAsync();
        } catch (Throwable e) {
            blf.ˋ.ˊ("VideoController", "playUrl error", e);
        }
    }

    public void onPrepared(MediaPlayer mediaPlayer) {
        blf.ˋ.ˎ("VideoController", "onPrepared");
        try {
            ॱ();
            ᐝ();
            if (this.ˊॱ != null) {
                this.ˏ.setClickable(true);
                this.ˊॱ.ͺ();
            }
        } catch (Throwable e) {
            blf.ˋ.ˊ("VideoController", "onPrepared error", e);
        }
    }

    public void onCompletion(MediaPlayer mediaPlayer) {
        blf.ˋ.ˎ("VideoController", "onCompletion");
        ˎ();
    }

    public void onBufferingUpdate(MediaPlayer mediaPlayer, int i) {
        if (this.ॱॱ != null) {
            this.ॱॱ.setSecondaryProgress(i);
        }
    }

    public boolean onError(MediaPlayer mediaPlayer, int i, int i2) {
        blf.ˋ.ˎ("VideoController", "onError, what:" + i + ", extra:" + i2);
        ॱॱ();
        boolean z = false;
        if (this.ˊॱ != null) {
            z = this.ˊॱ.ॱˊ();
        }
        ʼ();
        return z;
    }

    public void onClick(View view) {
        if (this.ʽ == view) {
            long currentTimeMillis = System.currentTimeMillis();
            if (currentTimeMillis - this.ʼ >= 1000) {
                this.ʼ = currentTimeMillis;
                if (this.ʽ.ॱ == e.playing) {
                    ˊ();
                } else {
                    ॱ();
                }
                ˋ();
                return;
            }
            return;
        }
        ᐝ();
    }

    private void ˋ() {
        this.ॱᐝ.removeMessages(1001);
        this.ॱᐝ.sendEmptyMessageDelayed(1001, 3000);
    }

    private void ʻ() {
        blf.ˋ.ˎ("VideoController", "hideController, released:" + this.ˋॱ + ", isCtrlDisplay:" + this.ͺ);
        if (!this.ˋॱ && this.ͺ) {
            try {
                this.ͺ = false;
                this.ॱᐝ.removeMessages(1000);
                this.ॱ.setVisibility(8);
            } catch (Throwable e) {
                blf.ˋ.ˊ("VideoController", "hideController error", e);
            }
        }
    }

    private void ᐝ() {
        blf.ˋ.ˎ("VideoController", "showController, released:" + this.ˋॱ + ", isCtrlDisplay:" + this.ͺ);
        if (!this.ˋॱ && !this.ͺ) {
            try {
                this.ͺ = true;
                this.ॱᐝ.sendEmptyMessage(1000);
                this.ॱ.setVisibility(0);
                this.ॱᐝ.sendEmptyMessageDelayed(1001, 3000);
            } catch (Throwable e) {
                blf.ˋ.ˊ("VideoController", "hideController error", e);
            }
        }
    }

    public void ॱ() {
        try {
            if (this.ˋ != null && this.ʽ.ॱ != e.playing) {
                this.ˋ.start();
                this.ʽ.setImageResource(d.ˊ);
                this.ʽ.ॱ = e.playing;
                if (this.ˊॱ != null) {
                    this.ˊॱ.I_();
                }
            }
        } catch (Throwable e) {
            blf.ˋ.ˊ("VideoController", "play error", e);
        }
    }

    public void ˊ() {
        try {
            if (this.ˋ != null && this.ʽ.ॱ == e.playing) {
                this.ˋ.pause();
                this.ʽ.ॱ = e.pause;
                this.ʽ.setImageResource(d.ॱ);
                if (this.ˊॱ != null) {
                    this.ˊॱ.ᐝॱ();
                }
            }
        } catch (Throwable e) {
            blf.ˋ.ˊ("VideoController", "play error", e);
        }
    }

    public void ˏ() {
        ॱॱ();
        ʼ();
    }

    public void ˎ() {
        ॱॱ();
        try {
            if (this.ˊॱ != null) {
                this.ˊॱ.ᐝ();
            }
        } catch (Throwable e) {
            blf.ˋ.ˊ("VideoController", "stop error", e);
        }
        ʼ();
    }

    private void ॱॱ() {
        try {
            if (this.ˋ != null) {
                this.ˋ.reset();
                this.ˋ.release();
                this.ˋ = null;
            }
        } catch (Throwable e) {
            blf.ˋ.ˊ("VideoController", "release error", e);
        }
    }

    private void ʼ() {
        this.ˋ = null;
        this.ˎ = null;
        this.ˋॱ = true;
        this.ͺ = false;
    }

    private void ʽ() {
        try {
            if (!this.ˋॱ && this.ˋ != null) {
                int currentPosition = this.ˋ.getCurrentPosition();
                int duration = this.ˋ.getDuration();
                if (duration > 0) {
                    long max = (((long) this.ॱॱ.getMax()) * ((long) currentPosition)) / ((long) duration);
                    this.ॱॱ.setProgress((int) max);
                    blf.ˋ.ˎ("VideoController", "setProgress, position:" + currentPosition + ", pos:" + max);
                }
                this.ᐝ.setText(ˋ(duration));
                this.ʻ.setText(ˋ(currentPosition));
            }
        } catch (Throwable e) {
            blf.ˋ.ˊ("VideoController", "setProgress error", e);
        }
    }

    private String ˋ(int i) {
        int i2 = i / 1000;
        int i3 = i2 % 60;
        int i4 = (i2 / 60) % 60;
        i2 /= 3600;
        this.ॱˊ.setLength(0);
        if (i2 > 0) {
            return this.ˏॱ.format("%d:%02d:%02d", new Object[]{Integer.valueOf(i2), Integer.valueOf(i4), Integer.valueOf(i3)}).toString();
        }
        return this.ˏॱ.format("%02d:%02d", new Object[]{Integer.valueOf(i4), Integer.valueOf(i3)}).toString();
    }

    public boolean onInfo(MediaPlayer mediaPlayer, int i, int i2) {
        return false;
    }

    public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i2, int i3) {
    }

    public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
        ॱॱ();
        ʼ();
    }
}
